package com.company.graph;

public class PowerEdge implements Comparable<PowerEdge> {
    //in power graph: v to w
    //in no-power graph: v - w
    int v;
    int w;
    int id;

    double power;
    private static final IdCounter counter = new IdCounter();

    public PowerEdge(int v, int w) {
        this.id = counter.generate();
        this.v = v;
        this.w = w;
        this.power = 0;
    }

    public PowerEdge(int v, int w, double power) {
        this.id = counter.generate();
        this.v = v;
        this.w = w;
        this.power = power;
    }

    public int getV() {
        return v;
    }

    public int getW() {
        return w;
    }

    public double getPower() {
        return power;
    }

    /**
     * 在无方向的条件下
     * 对比当前这条边是否与 v-w相同
     */
    public boolean isSameWithNodirection(int v, int w) {
        return this.v == v || this.v == w || this.w == v || this.w == w;
    }

    /**
     * 在有方向的条件下
     * 对比当前这条边是否与 v-w相同
     */
    public boolean isSameWithDirection(int v, int w) {
        return this.v == v && this.w == w;
    }

    public int other(Integer val) {
        if (val == v) {
            return w;
        } else if (val == w) {
            return v;
        } else {
            return -1;
        }
    }

    /**
     * @param node 该边是否包含结点node
     */
    public boolean hasNode(int node) {
        return v == node || w == node;
    }

    public int getId() {
        return this.id;
    }

    @Override
    public int compareTo(PowerEdge o) {
        return (int) (this.power - o.power);
    }

    static class IdCounter {
        int generateValue;

        public IdCounter() {
            this.generateValue = 1;
        }

        public int generate() {
            return generateValue++;
        }
    }

    @Override
    public String toString() {
        return "PowerEdge{" +
                "v=" + v +
                ", w=" + w +
                ", id=" + id +
                ", power=" + power +
                '}';
    }
}
